package com.pzj.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzj.dormitory.entity.Dormitory;
import com.pzj.dormitory.entity.Student;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.form.StudentForm;
import com.pzj.dormitory.mapper.DormitoryMapper;
import com.pzj.dormitory.mapper.StudentMapper;
import com.pzj.dormitory.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzj.dormitory.util.CommonUtil;
import com.pzj.dormitory.vo.PageVO;
import com.pzj.dormitory.vo.ResultVO;
import com.pzj.dormitory.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Override
    public boolean save(Student student) {
        // 添加学生数据
        // 入住时间
        student.setCreateDate(CommonUtil.createDate());
        int insert = studentMapper.insert(student);
        if (insert < 0) return false; // 添加失败
        // 修改宿舍数据
        // 查看是否满员
        Dormitory dormitory = dormitoryMapper.selectById(student.getDormitoryId());
        if (dormitory.getAvailable() == 0) return false;
        // 修改床位信息
        dormitory.setAvailable(dormitory.getAvailable() - 1);
        int i = dormitoryMapper.updateById(dormitory);
        if (i < 0) return false;
        return true;
    }

    @Override
    public PageVO list(Integer page, Integer pageSize) {
        Page<Student> studentPage = new Page<>(page, pageSize);
        Page<Student> page1 = studentMapper.selectPage(studentPage, null);
        List<Student> studentList = page1.getRecords();
        ArrayList<StudentVO> arrayList = new ArrayList<>();
        // VO转换
        studentList.forEach(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);
            Dormitory dormitory = dormitoryMapper.selectById(student.getDormitoryId());
            studentVO.setDormitoryName(dormitory.getName());
            arrayList.add(studentVO);
        });
        PageVO pageVO = new PageVO();
        pageVO.setData(arrayList);
        pageVO.setTotal(page1.getTotal());
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        // 模糊查询加分页
        Page<Student> objectPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        // 新建Page对象
        Page<Student> page = null;
        // 判断是否为空
        if (searchForm.getValue().equals("")) {
            page = studentMapper.selectPage(objectPage, null);
        } else {
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.like(searchForm.getKey(), searchForm.getValue());
            page = studentMapper.selectPage(objectPage, studentQueryWrapper);
        }
        List<Student> studentList = page.getRecords();
        ArrayList<StudentVO> arrayList = new ArrayList<>();
        // VO转换
        studentList.forEach(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);
            Dormitory dormitory = dormitoryMapper.selectById(student.getDormitoryId());
            studentVO.setDormitoryName(dormitory.getName());
            arrayList.add(studentVO);
        });
        PageVO pageVO = new PageVO();
        pageVO.setData(arrayList);
        pageVO.setTotal(page.getTotal());
        return pageVO;
    }

    @Override
    public ResultVO getStuById(Integer stuId) {
        Student student = studentMapper.selectById(stuId);
        StudentForm studentForm = new StudentForm();
        BeanUtils.copyProperties(student, studentForm);
        studentForm.setOldDormitoryId(student.getDormitoryId());
        ResultVO<StudentForm> studentVOResultVO = new ResultVO<>();
        studentVOResultVO.setData(studentForm);
        studentVOResultVO.setCode(0);
        return studentVOResultVO;
    }

    @Override
    public boolean updateStu(StudentForm studentForm) {
        Student student = new Student();
        BeanUtils.copyProperties(studentForm, student);
        System.out.println("student = " + student);
        int i = studentMapper.updateById(student);

        if (i < 0) return false;

        ResultVO<StudentForm> studentVOResultVO = null;
        if (!studentForm.getDormitoryId().equals(studentForm.getOldDormitoryId())) {
            try {
                dormitoryMapper.subAvailableById(studentForm.getDormitoryId());
                dormitoryMapper.addAvailableById(studentForm.getOldDormitoryId());
            } catch (Exception e) {
                return false;
            }
        }
        return true;

    }

    @Override
    public boolean deleteStu(Integer stuId) {
        Student student = studentMapper.selectById(stuId);
        Dormitory dormitory = dormitoryMapper.selectById(student.getDormitoryId());
        if (dormitory.getAvailable() < dormitory.getType()) {
            // 修改宿舍数据
            try {
                dormitoryMapper.addAvailableById(student.getDormitoryId());
            } catch (Exception e) {
                return false;
            }
        }

        // 删除学生数据
        int deleteById = studentMapper.deleteById(stuId);
        if (deleteById < 0) return false;

        return true;

    }
}
